package com.feisuanyz.lyweb01.service.impl;

import com.feisuanyz.lyweb01.dao.SolutionMapper;
import com.feisuanyz.lyweb01.pojo.domain.SolutionDO;
import com.feisuanyz.lyweb01.pojo.dto.SolutionDTO;
import com.feisuanyz.lyweb01.pojo.query.SolutionQuery;
import com.feisuanyz.lyweb01.pojo.vo.PageResult;
import com.feisuanyz.lyweb01.service.SolutionService;
import java.lang.Boolean;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.feisuanyz.lyweb01.exception.BusinessException;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.feisuanyz.lyweb01.constant.ResultCodeConstant;
import java.util.Date;

/**
 * 解决方案的实现
 *
 * @author deng
 * @date 2025-02-26 11:57:37
 */
@Service
public class SolutionServiceImpl implements SolutionService {

    @Autowired
    private SolutionMapper solutionMapper;

    @Override
    public PageResult<SolutionDO> getSolutionList(SolutionQuery query) {
        IPage<SolutionDO> page = new Page<>(query.getPageIndex(), query.getPageSize());
        IPage<SolutionDO> solutionPage = solutionMapper.selectPage(page, Wrappers.emptyWrapper());
        PageResult<SolutionDO> pageResult = new PageResult<>(solutionPage.getTotal(), solutionPage.getRecords());
        return pageResult;
    }

    @Override
    public SolutionDO getSolutionDetail(SolutionQuery query) {
        SolutionDO solutionDO = solutionMapper.selectById(query.getSolutionId());
        if (solutionDO == null) {
            throw new BusinessException(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG);
        }
        return solutionDO;
    }

    @Override
    public PageResult<SolutionDO> searchSolutions(SolutionQuery query) {
        QueryWrapper<SolutionDO> wrapper = Wrappers.query();
        wrapper.like("solution_name", query.getKeyword()).or().like("solution_description", query.getKeyword());
        IPage<SolutionDO> page = new Page<>(query.getPageIndex(), query.getPageSize());
        IPage<SolutionDO> solutionPage = solutionMapper.selectPage(page, wrapper);
        PageResult<SolutionDO> pageResult = new PageResult<>(solutionPage.getTotal(), solutionPage.getRecords());
        return pageResult;
    }

    @Override
    public Boolean addSolution(SolutionDTO solutionDTO) {
        SolutionDO solutionDO = new SolutionDO();
        solutionDO.setSolutionName(solutionDTO.getSolutionName());
        solutionDO.setSolutionDescription(solutionDTO.getSolutionDescription());
        solutionDO.setCreateBy(solutionDTO.getCreateBy());
        solutionDO.setCreateTime(new Date());
        solutionMapper.insert(solutionDO);
        return true;
    }

    @Override
    public Boolean updateSolution(SolutionDTO solutionDTO) {
        SolutionDO solutionDO = new SolutionDO();
        solutionDO.setSolutionId(solutionDTO.getSolutionId());
        solutionDO.setSolutionName(solutionDTO.getSolutionName());
        solutionDO.setSolutionDescription(solutionDTO.getSolutionDescription());
        solutionDO.setUpdateBy(solutionDTO.getUpdateBy());
        solutionDO.setUpdateTime(new Date());
        solutionMapper.updateById(solutionDO);
        return true;
    }

    @Override
    public Boolean deleteSolution(SolutionDTO solutionDTO) {
        solutionMapper.deleteById(solutionDTO.getSolutionId());
        return true;
    }
}
